package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.core.tools.file.PathUtils;
import vip.xiaonuo.smzq.modular.deom.ToolData;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Wjglb;
import vip.xiaonuo.smzq.modular.psba.entity.*;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Fyfxxtb;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlCount;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Yfjbxxtb;
import vip.xiaonuo.smzq.modular.psba.mapper.*;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.user.entity.Role;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.OrgMapper;
import vip.xiaonuo.smzq.modular.user.mapper.RoleMapper;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 评审备案申请记录表 服务实现类
 *
 * @author Kevin
 * @since 2024-06-11
 */
@Service
public class PsbaSqjlServiceImpl extends ServiceImpl<PsbaSqjlMapper, PsbaSqjl> implements IPsbaSqjlService {

    @Resource
    private PsbaSqjlMapper psbaSqjlMapper;
    @Resource
    private RoleMapper roleMapper; //角色表
    @Resource
    private UserMapper userMapper; //用户表

    @Resource
    private PsbaGtYfjbqkMapper psbaGtYfjbqkMapper; //通过mdb导入的压覆基本信息

    @Resource
    private PsbaGtJbqkMapper psbaGtJbqkMapper;//通过mdb导入的非压覆基本信息

    @Resource
    private PsbaSqjlYfjbqkMapper psbaSqjlYfjbqkMapper; //审批记录表-压覆基本信息

    @Resource
    private PsbaSqjlJbqkMapper psbaSqjlJbqkMapper;//审批记录表-非压覆基本信息

    @Resource
    private PsbaSqjlBgMapper psbaSqjlBgMapper;//审批记录表-报告

    @Resource
    private PsbaSqjlDztjMapper psbaSqjlDztjMapper;//审批记录表-地质条件

    @Resource
    private PsbaSqjlCzrzMapper psbaSqjlCzrzMapper;//操作日志

    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划

    //文件管理表
    @Resource
    private GtWjglbMapper gtWjglbMapper;

    @Override
    public Page<PsbaSqjl> page(PsbaSqjl psbaSqjl) {
        QueryWrapper<PsbaSqjl> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 压覆基本信息申请
     *
     * @param yfjbxxtb
     */
    @Override
    public void addYfjbqksq(Yfjbxxtb yfjbxxtb) {
        PsbaSqjl psbaSqjl = new PsbaSqjl();
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        String sxh = psbaGtYfjbqkMapper.getSxh();
        //压覆基本信息记录表
        PsbaSqjlYfjbqk psbaSqjlYfjbqk = yfjbxxtb.getPsbaSqjlYfjbqk();
        //压覆审批记录表
        String psbabh = ToolData.getPsbabh("0", psbaSqjlYfjbqk.getXzqdm(),sxh,"0");
        psbaSqjl.setPsbabh(psbabh);
        psbaSqjlYfjbqk.setPsbabh(psbabh);
        psbaSqjlYfjbqkMapper.insert(psbaSqjlYfjbqk);
        if (saBaseLoginUser != null) {
            psbaSqjl.setFqrxm(saBaseLoginUser.getName());
            psbaSqjl.setFqrid(saBaseLoginUser.getId());
            psbaSqjl.setFqsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        }
        psbaSqjl.setShzt("0");
        psbaSqjl.setShlx("0");
        psbaSqjl.setXzqhdm(psbaSqjlYfjbqk.getXzqdm());
        if (psbaSqjlYfjbqk.getXzqdm()!=null){
            QueryWrapper<Cb08Xzqh> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dm", Integer.valueOf(psbaSqjlYfjbqk.getXzqdm()));
            Cb08Xzqh cb08Xzqh = cb08XzqhMapper.selectOne(queryWrapper);
            psbaSqjl.setXzqmc(cb08Xzqh.getMc());
        }
        psbaSqjl.setBgwj(psbaSqjlYfjbqk.getBgwj());
        psbaSqjlMapper.insert(psbaSqjl);

        //添加一条填报日志
        PsbaSqjlCzrz psbaSqjlCzrz = new PsbaSqjlCzrz();
        psbaSqjlCzrz.setPsbajlId(psbabh);
        if (saBaseLoginUser != null) {
            psbaSqjlCzrz.setCzrid(saBaseLoginUser.getId());
            psbaSqjlCzrz.setCzrxm(saBaseLoginUser.getName());
        }
        psbaSqjlCzrz.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        psbaSqjlCzrz.setCzlx("1");
        psbaSqjlCzrzMapper.insert(psbaSqjlCzrz);

    }

    /**
     * 非压覆基本信息申请
     *
     * @param fyfxxtb
     */
    @Override
    public void addFyfjbqksq(Fyfxxtb fyfxxtb) {
        String nd = DateUtil.format(new Date(), "YYYY");
        String sxh = psbaGtJbqkMapper.getSxh(nd);
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();

        // 评审备案申请记录-非压覆基本情况
        PsbaSqjlJbqk psbaSqjlJbqk = fyfxxtb.getPsbaSqjlJbqk();
        String psbabh = ToolData.getPsbabh(fyfxxtb.getShlx(), saBaseLoginUser.getOrgId(),  sxh,"0");
        psbaSqjlJbqk.setPsbabh(psbabh);
        psbaSqjlJbqkMapper.insert(psbaSqjlJbqk);
        //非压覆审批记录表
        PsbaSqjl psbaSqjl = new PsbaSqjl();
        psbaSqjl.setPsbabh(psbabh);
        if (saBaseLoginUser != null) {
            psbaSqjl.setFqrxm(saBaseLoginUser.getName());
            psbaSqjl.setFqrid(saBaseLoginUser.getId());
            psbaSqjl.setFqsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        }
        psbaSqjl.setShzt("0");
        psbaSqjl.setShlx(fyfxxtb.getShlx());
        psbaSqjl.setXzqhdm(saBaseLoginUser.getOrgId());
        QueryWrapper<Cb08Xzqh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dm", Integer.valueOf(saBaseLoginUser.getOrgId()));
        Cb08Xzqh cb08Xzqh = cb08XzqhMapper.selectOne(queryWrapper);
        psbaSqjl.setXzqmc(cb08Xzqh.getMc());
        psbaSqjl.setBgwj(psbaSqjlJbqk.getBgwj());
        psbaSqjlMapper.insert(psbaSqjl);

        //非压覆报告信息
        PsbaSqjlBg psbaSqjlBg = fyfxxtb.getPsbaSqjlBg();
        psbaSqjlBg.setPsbabh(psbabh);
        psbaSqjlBgMapper.insert(psbaSqjlBg);
        //非压覆地质条件信息
        PsbaSqjlDztj psbaSqjlDztj = fyfxxtb.getPsbaSqjlDztj();
        psbaSqjlDztj.setPsbabh(psbabh);
        psbaSqjlDztjMapper.insert(psbaSqjlDztj);
        //添加一条填报日志
        PsbaSqjlCzrz psbaSqjlCzrz = new PsbaSqjlCzrz();
        psbaSqjlCzrz.setPsbajlId(psbabh);
        if (saBaseLoginUser != null) {
            psbaSqjlCzrz.setCzrid(saBaseLoginUser.getId());
            psbaSqjlCzrz.setCzrxm(saBaseLoginUser.getName());
        }
        psbaSqjlCzrz.setCzlx("1");
        psbaSqjlCzrz.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss"));
        psbaSqjlCzrzMapper.insert(psbaSqjlCzrz);
    }

    @Override
    public Page<PsbaSqjlVo> psbaSqjlList(long current, long size, String xzqhdm, String shzt,String fqrxm) {
        Page<PsbaSqjl> page = new Page<>(current, size);
        Page<PsbaSqjlVo> pageVo = new Page<>(current, size);

        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Role role = roleMapper.selectById(user.getPositionId());

        QueryWrapper<PsbaSqjl> queryWrapper = new QueryWrapper<>();
        if (role.getCode().equals("MIxjFfOLO6") || role.getCode().equals("zt7qwacif0")) {
            queryWrapper.eq("fqrid", saBaseLoginUser.getId());
        }

        if (fqrxm != null){
            queryWrapper.like("fqrxm", fqrxm);
        }

        if (role.getCode().equals("superAdmin")) {
            if (xzqhdm == null) {
                xzqhdm = "130000";
            }
            xzqhdm = xzqhdm.replaceAll("0+$", "");
           queryWrapper.like("xzqhdm", xzqhdm);
        } else {
            if (xzqhdm == null) {
                xzqhdm = saBaseLoginUser.getOrgId();
            }
            xzqhdm = xzqhdm.replaceAll("0+$", "");
            queryWrapper.like("xzqhdm", xzqhdm);
        }
        if (shzt != null) {
            queryWrapper.eq("shzt", shzt);
        }
        queryWrapper.orderByDesc("fqsj");
        page=  psbaSqjlMapper.selectPage(page, queryWrapper);
        List<PsbaSqjlVo> psbaSqjlVos = new ArrayList<>();
        for (PsbaSqjl psbaSqjl : page.getRecords()){
            PsbaSqjlVo psbaSqjlVo = new PsbaSqjlVo();
            BeanUtil.copyProperties(psbaSqjl,psbaSqjlVo);
              QueryWrapper<PsbaSqjlCzrz> queryWrapperCzrz = new QueryWrapper<>();
            queryWrapperCzrz.eq("psbajl_id",psbaSqjl.getPsbabh());
            queryWrapperCzrz.orderByDesc("czlx");
            queryWrapperCzrz.last("limit 1");
            PsbaSqjlCzrz psbaSqjlCzrz=psbaSqjlCzrzMapper.selectOne(queryWrapperCzrz);
            psbaSqjlVo.setNextNum(String.valueOf(Integer.parseInt(psbaSqjlCzrz.getCzlx())+1));
            psbaSqjlVo.setCzsj(psbaSqjlCzrz.getCzsj());
            psbaSqjlVo.setCzrxm(psbaSqjlCzrz.getCzrxm());
            psbaSqjlVos.add(psbaSqjlVo);

        }
        pageVo.setRecords(psbaSqjlVos);
        pageVo.setTotal(page.getTotal());
        pageVo.setCurrent(page.getCurrent());
        pageVo.setSize(page.getSize());
        return pageVo;
    }

    /**
     * 评审备案申请审批表分页查询
     *
     * @param current
     * @param size
     * @param xzqhdm
     * @param shzt
     * @param fqrxm
     * @author Kevin
     * @since 2024/7/18 14:50
     */
    @Override
    public Page<PsbaSqjlVo> psbaSqjlPage(long current, long size, String xzqhdm, String shzt, String fqrxm) {
        IPage<PsbaSqjlVo> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);

        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Role role = roleMapper.selectById(user.getPositionId());

        QueryWrapper<Map> queryWrapper = new QueryWrapper<>();
        if (role.getCode().equals("MIxjFfOLO6") || role.getCode().equals("zt7qwacif0")) {
            queryWrapper.eq("p.fqrid", saBaseLoginUser.getId());
        }

        if (fqrxm != null){
            queryWrapper.like("p.fqrxm", fqrxm);
        }

        if (role.getCode().equals("superAdmin")) {
            if (xzqhdm == null) {
                xzqhdm = "130000";
            }
            xzqhdm = xzqhdm.replaceAll("0+$", "");
            queryWrapper.like("p.xzqhdm", xzqhdm);
        } else {
            if (xzqhdm == null) {
                xzqhdm = saBaseLoginUser.getOrgId();
            }
            xzqhdm = xzqhdm.replaceAll("0+$", "");
            queryWrapper.like("p.xzqhdm", xzqhdm);
        }
        if (shzt != null) {
            queryWrapper.eq("p.shzt", shzt);
        }
        queryWrapper.orderByDesc("fqsj");

        return psbaSqjlMapper.psbaSqjlPage(iPage, queryWrapper);
    }

    /**
     * 评审备案申请审批表分页查询的数量查询
     * @param xzqhdm 行政区划代码
     * @param fqrxm 发起人名称
     */
    @Override
    public PsbaSqjlCount psbaSqjlCount(String xzqhdm, String fqrxm) {

        PsbaSqjlCount psbaSqjlCount=new PsbaSqjlCount();

        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Role role = roleMapper.selectById(user.getPositionId());

        QueryWrapper<PsbaSqjl> queryWrapper = new QueryWrapper<>();
        if (fqrxm != null){
            queryWrapper.like("fqrxm", fqrxm);
        }
        //判定是否为建设单位
        if(!role.getCode().equals("MIxjFfOLO6") ) {
            if (role.getCode().equals("superAdmin")) {
                if (xzqhdm == null) {
                    xzqhdm = "130000";
                }
                xzqhdm = xzqhdm.replaceAll("0+$", "");
                queryWrapper.like("xzqhdm", xzqhdm);
            }
            // 矿业权人
            else if(role.getCode().equals("zt7qwacif0")){
                queryWrapper.eq("fqrid", user.getId());
            }
            else {
                if (xzqhdm == null) {
                    xzqhdm = saBaseLoginUser.getOrgId();
                }
                xzqhdm = xzqhdm.replaceAll("0+$", "");
                queryWrapper.like("xzqhdm", xzqhdm);
            }
        }
        else{
            queryWrapper.eq("shlx", "0");
            queryWrapper.eq("fqrid", user.getId());
        }
        //全部数量
        Long allCount = psbaSqjlMapper.selectCount(queryWrapper);

        //审核中数量
        queryWrapper.eq("shzt", "0");
        Long auditCount = psbaSqjlMapper.selectCount(queryWrapper);

        //已完成数量
        Long finishCount=allCount-auditCount;

        psbaSqjlCount.setAllCount(allCount);
        psbaSqjlCount.setAuditCount(auditCount);
        psbaSqjlCount.setFinishCount(finishCount);

        return psbaSqjlCount;
    }

    /**
     * 评审备案报告文件预览
     *
     * @param fileId
     * @param response
     * @author Kevin
     * @since 2024/7/18 16:52
     */
    @Override
    public void fileShow(String fileId, HttpServletResponse response) {
        GtWjglb wjglb = gtWjglbMapper.selectById(fileId);
        PathUtils.singleFileShow(wjglb.getWjlj(), response);
    }
}
